package cn.juque.systemservice.convert;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.juque.common.utils.FastJsonUtil;
import cn.juque.systemservice.api.dto.org.OrgInfoDetailDTO;
import cn.juque.systemservice.domain.OrgInfoDO;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author juque
 * @version 1.0.0
 * <ul>
 *     <li>OrgConver</li>
 * </ul>
 * @date 2023-02-17 16:17:57
 **/
public class OrgConvertor {

    private OrgConvertor() {
    }

    public static List<OrgInfoDetailDTO> toOrgInfoDetailDtoList(List<OrgInfoDO> orgInfoList, Boolean containChild) {
        if(CollUtil.isEmpty(orgInfoList)) {
            return CollUtil.newArrayList();
        }
        return orgInfoList.stream().map(m->toOrgInfoDetailDTO(m, containChild)).collect(Collectors.toList());
    }

    public static OrgInfoDetailDTO toOrgInfoDetailDTO(OrgInfoDO orgInfoDO, Boolean containChild) {
        OrgInfoDetailDTO detailDTO = new OrgInfoDetailDTO();
        BeanUtil.copyProperties(orgInfoDO, detailDTO);
        if (Boolean.TRUE.equals(containChild)) {
            List<OrgInfoDetailDTO> children = FastJsonUtil.parseArray(FastJsonUtil.toJsonString(orgInfoDO.getChildren()), OrgInfoDetailDTO.class);
            detailDTO.setChildren(children);
        }
        return detailDTO;
    }
}
